<template>
  <div>
    <TopFilter :list="topFilterList" :cols="4" labelWidth="100" :collapse="false" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <!--<span>附加项目</span>-->
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :selectionType="'single'"
      :datakey="'rows'"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button>
        <!-- <el-button size="small">导出</el-button> -->
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visible" :title="dialogTitle" destroyOnClose width="500px" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <OrderPanel :type="handleType" :rowData="formData" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import OrderPanel from './OrderPanel';
import { getAddItem } from '@/api/repair/repair/addItem';
import Cookies from 'js-cookie';
export default {
  name: 'addItem',
  components: {
    OrderPanel
  },
  mixins: [dictionary],
  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '编辑', show: '显示' };
    return {
      topFilterList: this.createTopFilterList(),
      visible: false,
      fetchApi: getAddItem,
      dialogTitle: '',
      fetchParams: { xhrAbort: false },
      columns: this.createTableColumns(),
      handleType: '',
      formData: {}
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" disabled={this.isEdit(props.row)} onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },

        {
          title: '附加项目代码',
          dataIndex: 'addItemCode',
          width: 150,
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '附加项目名称',
          dataIndex: 'addItemName',
          width: 150,
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '附加项目费用',
          dataIndex: 'addItemAmount',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'number'
        },

        {
          title: '数据来源',
          dataIndex: 'dataSources',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1045'),
          render: props => {
            return <span>{this.createDictText(props.row.dataSources, '1045')}</span>;
          }
        },
        {
          title: '是否有效',
          dataIndex: 'isValid',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [
            { value: 10041001, text: '是' },
            { value: 10041002, text: '否' }
          ],
          render: props => {
            return <span>{this.changgeDictYN(props.row.isValid)}</span>;
          }
        }
      ];
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '附加项目代码',
          fieldName: 'addItemCode',
          placeholder: '附加项目代码...'
        },
        {
          type: 'INPUT',
          label: '附加项目名称',
          fieldName: 'addItemName',
          placeholder: '附加项目名称...'
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择',
          itemList: [
            { value: 10041001, text: '是' },
            { value: 10041002, text: '否' }
          ]
        }
      ];
    },
    isEdit(row) {
      // console.log(row.down_tag);
      if (row.dataSources == '10451002' && Cookies.get('dataType') == 10461001) {
        // 店端账号、并且对应主机厂数据时不可以编辑
        return true;
      } else {
        return false;
      }
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      this.handleType = 'edit';
      console.log(row.dataSources);
      this.formData = row;
      this.showHandler();
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    showHandler() {
      this.dialogTitle = `${this.conf[this.handleType]}附加项目信息`;
      this.visible = true;
    },
    changgeDictYN(val) {
      // console.log(val);
      if (val === 10041001) {
        return '是';
      } else {
        return '否';
      }
    },
    changgeDictLY(val) {
      console.log(val);
      if (val === 10451001) {
        return '店面';
      }
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
